package handler

import (
	"ball/api/request"
	"ball/internal/dao"
	"ball/internal/model"
	"ball/internal/queue"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddChat(c *gin.Context) {
	var req request.AddChatReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	chat := model.Chats{
		People: req.People,
		Test:   req.Test,
		Img:    req.Img,
	}
	if !dao.Create(&chat) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"data": nil,
		})
		return
	}
	queue.PublishPub("notification", chat)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"data": chat,
	})
	return
}
